2020-2021 Sunseeker Telemetry and Lighting System
timer_d_ex7_combineRegsPWM.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430F51x2 Demo - Timer0_D3, Combining two CCRx register to control one
34 // PWM channel; Normal Timer mode
35 //
36 // Description: This code example shows how to combine two CCRx registers to
37 // control both the rising and falling edges of one PWM channel (TD0.2, TD1.2).
38 // In up-mode, TDxCCR1 controls the rising edge and TDxCCR2 controls the
39 // falling edge. In this example, since CCR1 registers of TD0/1 have the same
40 // count, the rising edges at TD0.2 and TD1.2 happen almost at the same
41 // time instance. With TD0CCR2=40 and TD1CCR2=96, the dutycycles of the TD0.2
42 // and TD1.2 are 30% and 43.75%.
43 //
44 // ACLK = REF0; SMCLK = MCLK = default DCO ~1.045MHz.
45 //
46 // MSP430F51x2
47 // -----------------
48 // /|\| XIN|-
49 // | | |
50 // --|RST XOUT|-
51 // | |
52 // | P2.0/TD0.2|--> CCR2 - 30% duty cycle;
53 // | | ON period=(TD0CCR2-TD0CCR1)/32767 ~ 1.22ms
54 // | P2.3/TD1.2|--> CCR2 - 43.75% duty cycle;
55 // | | ON period=(TD1CCR2-TD1CCR1)/32767 ~ 1.7ms
56 //
57 //******************************************************************************
58 #include "driverlib.h"
59 
60 void main(void)
61 {
62  WDT_A_hold(WDT_A_BASE);
63 
64  // Set port pins
65  GPIO_setAsPeripheralModuleFunctionOutputPin(
66  GPIO_PORT_P2,
67  GPIO_PIN0 + GPIO_PIN3
68  );
69 
70 
71  // Configure TimerD0 to combine CCR0/1 block to control TD0CCR2 PWM output
72  Timer_D_combineTDCCRToOutputPWMParam pwm1Param = {0};
73  pwm1Param.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
74  pwm1Param.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
75  pwm1Param.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
76  pwm1Param.timerPeriod = 128;
77  pwm1Param.combineCCRRegistersCombination = TIMER_D_COMBINE_CCR1_CCR2;
78  pwm1Param.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
79  pwm1Param.dutyCycle1 = 40;
80  pwm1Param.dutyCycle2 = 80;
81  Timer_D_combineTDCCRToOutputPWM(TIMER_D0_BASE, &pwm1Param);
82 
83  // Configure TimerD1 to combine CCR0/1 block to control TD1CCR2 PWM output
84  Timer_D_combineTDCCRToOutputPWMParam pwm2Param = {0};
85  pwm2Param.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
86  pwm2Param.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
87  pwm2Param.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
88  pwm2Param.timerPeriod = 128;
89  pwm2Param.combineCCRRegistersCombination = TIMER_D_COMBINE_CCR1_CCR2;
90  pwm2Param.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
91  pwm2Param.dutyCycle1 = 40;
92  pwm2Param.dutyCycle2 = 96;
93  Timer_D_combineTDCCRToOutputPWM(TIMER_D1_BASE, &pwm2Param);
94 
95  Timer_D_initUpModeParam initUp1Param = {0};
96  initUp1Param.clockSource = TIMER_D_CLOCKSOURCE_ACLK;
97  initUp1Param.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
98  initUp1Param.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
99  initUp1Param.timerPeriod = 128-1;
100  initUp1Param.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
101  initUp1Param.captureCompareInterruptEnable_CCR0_CCIE =
102  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
103  initUp1Param.timerClear = TIMER_D_DO_CLEAR;
104  Timer_D_initUpMode(TIMER_D0_BASE, &initUp1Param);
105 
106  Timer_D_initUpModeParam initUp2Param = {0};
107  initUp2Param.clockSource = TIMER_D_CLOCKSOURCE_ACLK;
108  initUp2Param.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
109  initUp2Param.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
110  initUp2Param.timerPeriod = 128-1;
111  initUp2Param.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
112  initUp2Param.captureCompareInterruptEnable_CCR0_CCIE =
113  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
114  initUp2Param.timerClear = TIMER_D_DO_CLEAR;
115  Timer_D_initUpMode(TIMER_D1_BASE, &initUp2Param);
116 
117  Timer_D_startCounter(TIMER_D0_BASE,
118  TIMER_D_UP_MODE
119  );
120  Timer_D_startCounter(TIMER_D1_BASE,
121  TIMER_D_UP_MODE
122  );
123 
124  __bis_SR_register(LPM0_bits); // Enter LPM0
125  __no_operation(); // For debugger
126 
127 }
__no_operation()
void main(void)